#ifndef DOTCONTROLLER_H
#define DOTCONTROLLER_H

#include <QObject>
#include <QPoint>
#include <QTimer>
#include "../DrawingElements/Line.h"
#include <QPropertyAnimation>
#include "../AI/AILineSelectorAlgMax.h"
#include "../AI/AILineSelectorAlgMin.h"

class Dot;
class DotModel;
class DotView;
class QMouseEvent;
class QPainter;
class ComputerPlay;
class Connection;
class NetworkManager;
class BuddyWnd;
class PlayRequestMsg;
class PlayRequestResultMsg;
class GameCanceledMsg;
class ControlPanel;
class LineAddedMsg;

class DotController : public QObject
{
Q_OBJECT
public:
    DotController(DotView *pView, DotModel *pModel, QObject *parent = 0);
    ~DotController();

public:    
    DotModel *model()const;
    DotView *view()const;        
    Dot *findDotNearToPoint(QPoint pos);
    void mousePressEvent(QMouseEvent *pEvent);
    bool isEligibleForLine(Dot *pDot1, Dot *pDot2);
    bool isGameStarted();
    bool isGameRunning();
    bool isGameFinished();
    Game::Player winner();
    Game::Player loser();
    Game::Player gameTurner();
    void draw(QPainter *pDc);
    bool startGame(Game::Player turnPlayer = Game::Unknown);
    bool cancelGame(bool bShowMsg = true,bool bSendMsg = false);
    NetworkManager *netManager();
    void setCtrlPanel(ControlPanel *pPnl);

signals:
    void gameCanceled();
    void gameStarted();
    void lineAdded(Game::Player owner);
    void cellOwned(Game::Player owner);
    void gameFinished(Game::Player winner);
    void gameTurnChanged(Game::Player player);
    void networkGamePlayingViewInfoChanged(QString playerName,QColor color);
    void networkGameStartingParameterRequest(int nDotsPerRow, int nDotsPerColumn,QString playerName,QColor color);

public slots:
    void posAnimationFinished();
    void turnChanged(Game::Player player);

private slots:
    void askForPlaying(Connection *pToWhom);
    void peerLeft(Connection *pWho);
    void playRequestCame(Connection *pFrom,PlayRequestMsg *pMsg);
    void playRequestResultCame(Connection *pFrom,PlayRequestResultMsg *pMsg);
    void gameCanceledMsgCame(Connection *pFrom,GameCanceledMsg *pMsg);
    void lineAddedMsgCame(Connection *pFrom,LineAddedMsg *pMsg);
    void playingPeerViewInfoChanged();

private:
    void  aDotSelected(Dot *pDot);
    void aDotClicked(QPoint pos);
    void resumeComputerPlaying();
    void logThisGame();
    void addALine(Dot *pDot1,Dot *pDot2);
    void setPlayingPeer(Connection *pPeer);

private:
    DotView *mpView;
    DotModel *mpModel;
    Dot *mpDotSelected;
    Game::Player mGameTurn;
    bool mbGameStarted,mbGameFinshed;
    QList<Dot*> mDotsSelectable;
    QTimer mTimer;
    uint mnRndRadius;
    ComputerPlay *mpComputerPlay;
    QPropertyAnimation *mpPosAnimation;
    AILineSelectorAlgMax *mpAISelectorMax;
    AILineSelectorAlgMin *mpAISelectorMin;
    bool mbPalyingWithComputer;
    NetworkManager *mpNetManager;
    Connection *mpPeerPlayingWith,*mpPeerSentRequest;
    ControlPanel *mpCtrlPnl;
    QLine mLastLineAddeByPeer;
};

#endif // DOTCONTROLLER_H
